#include <QtGui>
#include <QObject>

#include "mainwindow.h"
#include "infowin.h"

GraphWin::GraphWin(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) {
	setMinimumWidth(800);
	setFixedHeight(600);
	
	menuBar = new QMenuBar(this);
	fileMenu = menuBar->addMenu(tr("File"));
	
	createActions();
	
	fileMenu->addAction(openAct);
	fileMenu->addAction(aboutAct);
	fileMenu->addAction(printAct);
	fileMenu->addSeparator();
	fileMenu->addAction(quitAct);	

	createCalendar();
	
	scrollArea = new Scroll(this);
	gw = new GraphWid(scrollArea);
	scrollArea->setWidget(gw);
	scrollArea->setWidgetResizable(true);		

	QPushButton *closeButton = new QPushButton(tr("&Close"));
	QPushButton *infoButton = new QPushButton(tr("Info"));
	infoButton->hide();
	connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
	connect(infoButton, SIGNAL(clicked()), this, SLOT(info()));
		

	hrLabel = new QLabel(this);
	hrLabel->setNum(0);
	QLabel *hrLabelMis = new QLabel(this);
	hrLabelMis->setText("bpm");
	connect(gw, SIGNAL(hrChanged(int)), hrLabel, SLOT(setNum(int)));
	
	speedLabel = new QLabel(this);
	speedLabel->setNum(0);
	QLabel *speedLabelMis = new QLabel(this);
	speedLabelMis->setText("km/h");
	connect(gw, SIGNAL(speedChanged(double)), speedLabel, SLOT(setNum(double)));	

	altLabel = new QLabel(this);
	altLabel->setNum(0);
	QLabel *altLabelMis = new QLabel(this);
	altLabelMis->setText("m");
	connect(gw, SIGNAL(altChanged(int)), altLabel, SLOT(setNum(int)));

	crono = new QLabel(this);
	crono->setText("00:00:00");
	connect(gw, SIGNAL(cronoChanged(int)), this, SLOT(cronoChange(int)));	

	hrLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	speedLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	altLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	crono->setFrameStyle(QFrame::Panel | QFrame::Sunken);

	playButton = new QPushButton("Auto", this);
	playButton->setShortcut(Qt::Key_Space);
	playButton->setCheckable(true);
	connect(playButton, SIGNAL(clicked(bool)), scrollArea, SLOT(play(bool)));

	misBox = new QGroupBox();
	
	QHBoxLayout *hMisbox = new QHBoxLayout;
	hMisbox->addWidget(hrLabel);
	hMisbox->addWidget(hrLabelMis);
	hMisbox->addWidget(speedLabel);
	hMisbox->addWidget(speedLabelMis);
	hMisbox->addWidget(altLabel);
	hMisbox->addWidget(altLabelMis);
	hMisbox->addWidget(crono);
	hMisbox->addWidget(playButton);
	misBox->setLayout(hMisbox);

	connect(scrollArea, SIGNAL(notScroll(bool)), misBox, SLOT(setEnabled(bool)));
	
	QVBoxLayout *barLayout = createIcons();
	
	QHBoxLayout *buttonsLayout = new QHBoxLayout;
	
	buttonsLayout->addWidget(misBox);
	
	buttonsLayout->addStretch(1);
	buttonsLayout->addWidget(infoButton);
	buttonsLayout->addWidget(closeButton);
	
	QVBoxLayout *verticalLayout = new QVBoxLayout;
	verticalLayout->addWidget(pagesWidget);
	verticalLayout->addSpacing(12);
	verticalLayout->addLayout(buttonsLayout);
	
	QGridLayout *mainLayout = new QGridLayout;
	mainLayout->addWidget(menuBar, 1, 1, 1, 2);
	mainLayout->addLayout(barLayout, 2, 1);
	mainLayout->addLayout(verticalLayout, 2, 2); 
	setLayout(mainLayout);
	
}

void GraphWin::cronoChange(int newValue)
{
	int s = newValue % 60;
	int m = (newValue / 60) % 60;
	int h = newValue / 3600;
	crono->setText(QTime(h, m, s).toString("hh:mm:ss"));
}

void GraphWin::createCalendar() {
	calWid = new Calendar(this);	
	calWid->setFirstDayOfWeek(Qt::Monday);
	QPainter *painter = new QPainter;
	painter->drawLine(10, 10, 20, 20);
}


QVBoxLayout* GraphWin::createIcons() {
	QPushButton *graphsButton =
		new QPushButton(QIcon(":/images/heartrate.png"), QString("Graphs"), this);
	graphsButton->setIcon(QIcon(":/images/heartrate.png"));
	//graphsButton->setIconSize(QSize(61, 50));
	graphsButton->setShortcut(tr("Ctrl+1"));

	QCheckBox *altButton =
		new QCheckBox("Altitude", this);
	connect(altButton, SIGNAL(clicked(bool)), gw, SLOT(setAltVisible(bool)));
	connect(altButton, SIGNAL(clicked(bool)), altLabel, SLOT(setEnabled(bool)));
	altButton->setCheckState(Qt::Checked);

	QCheckBox *hrButton =
		new QCheckBox("Heart rate", this);
	connect(hrButton, SIGNAL(clicked(bool)), gw, SLOT(setHRVisible(bool)));
	connect(hrButton, SIGNAL(clicked(bool)), hrLabel, SLOT(setEnabled(bool)));
	connect(hrButton, SIGNAL(clicked(bool)), playButton, SLOT(setEnabled(bool)));
	hrButton->setCheckState(Qt::Checked);

	QCheckBox *speedButton =
		new QCheckBox("Speed", this);
	connect(speedButton, SIGNAL(clicked(bool)), gw, SLOT(setSpeedVisible(bool)));
	connect(speedButton, SIGNAL(clicked(bool)), speedLabel, SLOT(setEnabled(bool)));
	speedButton->setCheckState(Qt::Checked);

	QVBoxLayout *boxLayout = new QVBoxLayout;
	boxLayout->addWidget(altButton);
	boxLayout->addWidget(hrButton);
	boxLayout->addWidget(speedButton);

	graphBox = new QGroupBox;
	graphBox->setLayout(boxLayout);

	QPushButton *histButton =
		new QPushButton("Histogram", this);
	histButton->setIcon(QIcon(":/images/histogram.png"));
	//histButton->setIconSize(QSize(61, 50));
	//histButton->setShortcut(tr("Ctrl+2"));

	histButton->hide();

	QPushButton *calButton =
		new QPushButton("Calendar", this);
	calButton->setIcon(QIcon(":/images/cal.png"));
	//calButton->setIconSize(QSize(61, 50));
	calButton->setShortcut(tr("Ctrl+2"));

	QObject::connect(graphsButton, SIGNAL(clicked()),
		graphBox, SLOT(show()));
	QObject::connect(graphsButton, SIGNAL(clicked()),
		misBox, SLOT(show()));
	QObject::connect(histButton, SIGNAL(clicked()),
		graphBox, SLOT(hide()));
	QObject::connect(calButton, SIGNAL(clicked()),
		graphBox, SLOT(hide()));
	QObject::connect(calButton, SIGNAL(clicked()),
		misBox, SLOT(hide()));

	pagesWidget = new QStackedWidget;
	pagesWidget->addWidget(scrollArea);
	pagesWidget->addWidget(calWid);

	QObject::connect(graphsButton, SIGNAL(clicked()),
		this, SLOT(setGraphWid()));

/*
QObject::connect(histButton, SIGNAL(clicked()),
this, SLOT(setCentalWid()));
*/
	QObject::connect(calButton, SIGNAL(clicked()),
		this, SLOT(setCalWid()));

	QVBoxLayout *barLayout = new QVBoxLayout;
	barLayout->addWidget(graphsButton);
	barLayout->addWidget(graphBox);
	barLayout->addWidget(histButton);
	barLayout->addWidget(calButton);
	barLayout->addStretch(1);
	return barLayout;

}

void GraphWin::createActions() 
{
  aboutAct = new QAction(tr("&About"), this);
  connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
  
  openAct = new QAction(tr("&Open..."), this);
  openAct->setShortcut(tr("Ctrl+O"));
  openAct->setStatusTip(tr("Open an exercise file"));
  connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

  printAct = new QAction(tr("&Print"), this);
  connect(printAct, SIGNAL(triggered()), this, SLOT(print()));

  quitAct = new QAction(tr("&Quit"), this);
  connect(quitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  
}

void GraphWin::print()
{
	QPrinter printer;

	QPrintDialog *dialog = new QPrintDialog(&printer, this);
	dialog->setWindowTitle(tr("Print"));
	if (dialog->exec() != QDialog::Accepted)
		return;
	gw->print(&printer);
}

void GraphWin::about()
{
  QMessageBox about;
  about.setIconPixmap(QPixmap(":/images/logo.png"));
  about.setWindowTitle("About KPant");
  about.setText("<h3>KPant (Training Software) <br> version 0.1</h3>");
  about.setInformativeText("&copy; 2008 Alessandro Cosentino<br>"
			   "distributed under GPLv3 License");
  about.exec();	
}

void GraphWin::open()
{
  QString fileName = QFileDialog::getOpenFileName(
						  this,
						  "Select one .hrm file",
						  QDir::homePath (),
						  "Exercises (*.hrm)");
  if(!fileName.isNull()) {
    ((MainWindow*)parent())->loadFile(fileName);
  }
}

void GraphWin::setCalWid() {
	pagesWidget->setCurrentWidget(calWid);
	graphBox->hide();
}

void GraphWin::setGraphWid() {
	pagesWidget->setCurrentWidget(scrollArea);
	graphBox->show();
}

void GraphWin::setData(DataHRM *data) {
	gw->setData(data);
}

void GraphWin::closeEvent(QCloseEvent *) {
	MainWindow* mw = (MainWindow*)parentWidget();
	mw->unload();
}

void GraphWin::info() {
	InfoWin *infodialog = new InfoWin(this, Qt::Sheet);
	infodialog->exec();
}
